package com.tgu.home_action.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tgu.home_action.dao.*;
import com.tgu.home_action.pojo.*;
import com.tgu.home_action.pojo.state.ResponseState;
import com.tgu.home_action.service.MarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MarkServiceImpl implements MarkService {
    @Autowired
    private MarkMapper markMapper;
    @Autowired
    private CaseMapper caseMapper;
    @Autowired
    private VolunteerInformationMapper volunteerInformationMapper;
    @Autowired
    private FamilyMapper familyMapper;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    /**
     * 志愿者添加标记
     */
    @Override
    public ResponseData<Mark> volunteerAdd(Mark mark, Volunteer volunteer) {
        Case nowCase=caseMapper.getById(mark.getCaseId());
        if(nowCase==null){
            return new ResponseData<>(ResponseState.CASE_NOT_EXIST.getMessage(), ResponseState.CASE_NOT_EXIST.getValue());
        }
        String userName=volunteerInformationMapper.getNameByVolunteerId(volunteer.getId());
        mark.setUserName(userName);
        mark.setRole("volunteer");
        mark.setVolunteerId(volunteer.getId());
        markMapper.insert(mark);
        simpMessagingTemplate.convertAndSend("/case/"+mark.getCaseId(),new ResponseData<Mark>(ResponseState.NEW_MARK.getMessage(), ResponseState.NEW_MARK.getValue(),mark));
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(),mark);
    }

    /**
     * 家属添加标记
     */
    @Override
    public ResponseData<Object> familyAdd(Mark mark, Family family) {
        Case nowCase=caseMapper.getById(mark.getCaseId());
        if(nowCase==null){
            return new ResponseData<>(ResponseState.CASE_NOT_EXIST.getMessage(), ResponseState.CASE_NOT_EXIST.getValue());
        }
        family=familyMapper.getById(family.getId());
        mark.setUserName(family.getName());
        mark.setRole("family");
        mark.setFamilyId(family.getId());
        markMapper.insert(mark);
        simpMessagingTemplate.convertAndSend("/case/"+mark.getCaseId(),new ResponseData<Mark>(ResponseState.NEW_MARK.getMessage(), ResponseState.NEW_MARK.getValue(),mark));
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(),mark);
    }

    /**
     * 管理员添加标记
     * @return
     */
    @Override
    public ResponseData<Mark> adminAdd(Mark mark, Admin admin) {
        Case nowCase=caseMapper.getById(mark.getCaseId());
        if(nowCase==null){
            return new ResponseData<>(ResponseState.CASE_NOT_EXIST.getMessage(), ResponseState.CASE_NOT_EXIST.getValue());
        }
        admin=adminMapper.getById(admin.getId());
        mark.setUserName(admin.getUserName());
        mark.setRole("admin");
        mark.setAdminId(admin.getId());
        markMapper.insert(mark);
        simpMessagingTemplate.convertAndSend("/case/"+mark.getCaseId(),new ResponseData<Mark>(ResponseState.NEW_MARK.getMessage(), ResponseState.NEW_MARK.getValue(),mark));
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(),mark);
    }

    /**
     * 查询标记
     */
    @Override
    public ResponseData<List<Mark>> get(Mark mark) {
        QueryWrapper<Mark> markQueryWrapper=new QueryWrapper<>();
        if(mark.getId()!=null){
            markQueryWrapper.eq("id",mark.getId());
        }
        if(mark.getCaseId()!=null){
            markQueryWrapper.eq("case_id",mark.getCaseId());
        }
        if(mark.getVolunteerId()!=null){
            markQueryWrapper.eq("volunteer_id",mark.getVolunteerId());
        }
        if(mark.getAdminId()!=null){
            markQueryWrapper.eq("admin_id",mark.getAdminId());
        }
        if(mark.getRole()!=null){
            markQueryWrapper.eq("role",mark.getRole());
        }
        if(mark.getName()!=null){
            markQueryWrapper.like("name","%"+mark.getName()+"%");
        }
        List<Mark> marks=markMapper.selectList(markQueryWrapper);
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(),marks);
    }

    /**
     * 志愿者删除标记
     */
    @Override
    public ResponseData<Object> volunteerDel(Integer id, Volunteer volunteer) {
        Mark mark=markMapper.selectById(id);
        if(mark==null){
            return new ResponseData<>(ResponseState.MARK_NOT_EXIST.getMessage(),ResponseState.MARK_NOT_EXIST.getValue());
        }
        if(!mark.getRole().equals("volunteer")){
            return new ResponseData<>(ResponseState.WITHOUT_PERMISSION.getMessage(), ResponseState.WITHOUT_PERMISSION.getValue());
        }
        if(!mark.getVolunteerId().equals(volunteer.getId())){
            return new ResponseData<>(ResponseState.WITHOUT_PERMISSION.getMessage(), ResponseState.WITHOUT_PERMISSION.getValue());
        }
        markMapper.deleteById(id);
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue());
    }

    /**
     * 家属删除标记
     */
    @Override
    public ResponseData<Object> familyDel(Integer id, Family family) {
        Mark mark=markMapper.selectById(id);
        if(mark==null){
            return new ResponseData<>(ResponseState.MARK_NOT_EXIST.getMessage(),ResponseState.MARK_NOT_EXIST.getValue());
        }
        if(!mark.getRole().equals("family")){
            return new ResponseData<>(ResponseState.WITHOUT_PERMISSION.getMessage(), ResponseState.WITHOUT_PERMISSION.getValue());
        }
        if(!mark.getFamilyId().equals(family.getId())){
            return new ResponseData<>(ResponseState.WITHOUT_PERMISSION.getMessage(), ResponseState.WITHOUT_PERMISSION.getValue());
        }
        markMapper.deleteById(id);
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue());
    }

    /**
     * 管理员删除标记
     */
    @Override
    public ResponseData<Object> adminDel(Integer id) {
        Mark mark=markMapper.selectById(id);
        if(mark==null){
            return new ResponseData<>(ResponseState.MARK_NOT_EXIST.getMessage(),ResponseState.MARK_NOT_EXIST.getValue());
        }
        markMapper.deleteById(id);
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue());
    }

    /**
     * 志愿者修改标记
     * @return
     */
    @Override
    public ResponseData<Mark> volunteerUpdate(Mark mark, Volunteer volunteer) {
        Mark oldMark=markMapper.selectById(mark.getId());
        if(oldMark==null){
            return new ResponseData<>(ResponseState.MARK_NOT_EXIST.getMessage(),ResponseState.MARK_NOT_EXIST.getValue());
        }
        if(!oldMark.getRole().equals("volunteer")){
            return new ResponseData<>(ResponseState.WITHOUT_PERMISSION.getMessage(), ResponseState.WITHOUT_PERMISSION.getValue());
        }
        if(!oldMark.getVolunteerId().equals(volunteer.getId())){
            return new ResponseData<>(ResponseState.WITHOUT_PERMISSION.getMessage(), ResponseState.WITHOUT_PERMISSION.getValue());
        }
        markMapper.updateById(mark);
        oldMark=markMapper.selectById(mark.getId());
        simpMessagingTemplate.convertAndSend("/case/"+oldMark.getCaseId(),new ResponseData<>(ResponseState.MARK_UPDATE.getMessage(), ResponseState.NEW_MARK.getValue(),oldMark));
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue());
    }

    /**
     * 家属修改标记
     */
    @Override
    public ResponseData<Object> familyUpdate(Mark mark, Family family) {
        Mark oldMark=markMapper.selectById(mark.getId());
        if(oldMark==null){
            return new ResponseData<>(ResponseState.MARK_NOT_EXIST.getMessage(),ResponseState.MARK_NOT_EXIST.getValue());
        }
        if(!oldMark.getRole().equals("family")){
            return new ResponseData<>(ResponseState.WITHOUT_PERMISSION.getMessage(), ResponseState.WITHOUT_PERMISSION.getValue());
        }
        if(!oldMark.getFamilyId().equals(family.getId())){
            return new ResponseData<>(ResponseState.WITHOUT_PERMISSION.getMessage(), ResponseState.WITHOUT_PERMISSION.getValue());
        }
        markMapper.updateById(mark);
        oldMark=markMapper.selectById(mark.getId());
        simpMessagingTemplate.convertAndSend("/case/"+oldMark.getCaseId(),new ResponseData<>(ResponseState.MARK_UPDATE.getMessage(), ResponseState.NEW_MARK.getValue(),oldMark));
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue());
    }

    /**
     * 管理员修改标记
     */
    @Override
    public ResponseData<Object> adminUpdate(Mark mark, Admin admin) {
        Mark oldMark=markMapper.selectById(mark.getId());
        if(oldMark==null){
            return new ResponseData<>(ResponseState.MARK_NOT_EXIST.getMessage(),ResponseState.MARK_NOT_EXIST.getValue());
        }
        markMapper.updateById(mark);
        oldMark=markMapper.selectById(mark.getId());
        simpMessagingTemplate.convertAndSend("/case/"+oldMark.getCaseId(),new ResponseData<>(ResponseState.MARK_UPDATE.getMessage(), ResponseState.NEW_MARK.getValue(),oldMark));
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue());
    }
}
